//单个屏，2.8寸电阻触摸屏 ili9341



#include <bb_spi_lcd.h>
#include "JPEGDEC.h"
//#include "greenbarh.h"
#include "fbb.h"
#include "ldh2.h"
#include "phobe2_1.h"
#include "zhm.h"
#include "zxc.h"
#include "zy.h"
#include "zz.h"
#include "gbar.h"
#include "jie.h"
#include "red.h"
//#include "img.h"
#include "red16.h"

// are left to you
#define TFT_CS  19  //15
#define TFT_RST 18  //12
#define TFT_DC  23      //2
#define TFT_MOSI 5 //13  // Data out
#define TFT_SCLK  17 //    14  // Clock out
#define TFT_BL  22 //     21 //背光

#define NTP1 "ntp1.aliyun.com"
#define NTP2 "ntp2.aliyun.com"
#define NTP3 "ntp3.aliyun.com"

JPEGDEC jpeg;
// The LCD display library instance
SPILCD lcd;

#define PicNum sizeof(pics)/sizeof(pics[0])
//#define 
typedef struct {
  uint8_t* img;
  int size;
} ImgInfo;

#define IMG(x) {(uint8_t*)x,sizeof(x)}

ImgInfo pics[]={IMG(fbb),IMG(ldh2),IMG(phobe2_1),IMG(zhm),IMG(zxc),IMG(zy),IMG(zz),IMG(gbar),IMG(jie),IMG(red)};

int icount=0;
struct tm timeinfo_last;

int drawMCUs(JPEGDRAW *pDraw)
{
  int iCount;
  iCount = pDraw->iWidth * pDraw->iHeight; // number of pixels to draw in this call
//  Serial.printf("Draw pos = %d,%d. size = %d x %d\n", pDraw->x, pDraw->y, pDraw->iWidth, pDraw->iHeight);
  spilcdSetPosition(&lcd, pDraw->x, pDraw->y, pDraw->iWidth, pDraw->iHeight, DRAW_TO_LCD);
  spilcdWriteDataBlock(&lcd, (uint8_t *)pDraw->pPixels, iCount*2, DRAW_TO_LCD | DRAW_WITH_DMA);
  
  return 1; // returning true (1) tells JPEGDEC to continue decoding. Returning false (0) would quit decoding immediately.
} /* drawMCUs() */

void drawPixels(SPILCD* plcd,uint8_t* ppixels, int x,int y,int w,int h)
{
  int iCount;
  iCount = w * h; // number of pixels to draw in this call
//  Serial.printf("Draw pos = %d,%d. size = %d x %d\n", pDraw->x, pDraw->y, pDraw->iWidth, pDraw->iHeight);
  spilcdSetPosition(plcd, x, y, w, h, DRAW_TO_LCD);
  spilcdWriteDataBlock(plcd, ppixels, iCount*2, DRAW_TO_LCD | DRAW_WITH_DMA);
}


void setup()
{
  Serial.begin(115200);
  pinMode(TFT_CS,OUTPUT);
  // pinMode(TFT_CS2,OUTPUT);
  // pinMode(TFT_CS3,OUTPUT);
  // pinMode(TFT_CS4,OUTPUT);

  // pinMode(TFT_RST,OUTPUT);
  // pinMode(TFT_RST2,OUTPUT);
  // pinMode(TFT_RST3,OUTPUT);
  // pinMode(TFT_RST4,OUTPUT);

  resetDisps();
  //initScreens();
  //enableAll();

  // ST7735R
  spilcdInit(&lcd, LCD_ST7735R, FLAGS_NONE, 40000000,   TFT_CS, TFT_DC, TFT_RST, TFT_BL, -1, TFT_MOSI, TFT_SCLK,1); 
  //LCD_ILI9341, 
  //spilcdInit(&lcd, LCD_ILI9341, FLAGS_NONE, 40000000,   TFT_CS, TFT_DC, TFT_RST, TFT_BL, -1, TFT_MOSI, TFT_SCLK,1);
  delay(1000);

  Serial.println("draw a bmp:");
  drawPixels(&lcd,(uint8_t*)green16,0,0,10,10);
  delay(5000);


  
  //spilcdInit(&lcd2, LCD_ST7735R, FLAGS_NONE, 40000000,   18, 32, 23, 33, -1, 25, 26,1); // ST7735R
  // spilcdInit(&lcd3, LCD_ST7735R, FLAGS_NONE, 40000000,   17, 32, 5, 33, -1, 25, 26,1); // ST7735R
  // spilcdInit(&lcd4, LCD_ST7735R, FLAGS_NONE, 40000000,   4, 32, 16, 33, -1, 25, 26,1); // ST7735R
  //spilcdSetOrientation(&lcd, LCD_ORIENTATION_90);
  spilcdFill(&lcd, 0, DRAW_TO_LCD); // erase display to black
  //spilcdWriteString(&lcd, 0,0,(char *)"Bruce Timer", 0x7e0,0,FONT_8x8, DRAW_TO_LCD);
  spilcdWriteString(&lcd, 0,0,(char *)"Beautiful", 0xffff,0,FONT_8x8, DRAW_TO_LCD);
  delay(3000);
  
  //spilcdFill(&lcd2, 0, DRAW_TO_LCD); // erase display to black
  // spilcdFill(&lcd3, 0, DRAW_TO_LCD); // erase display to black
  // spilcdFill(&lcd4, 0, DRAW_TO_LCD); // erase display to black
  

  //屏幕初始化完成
  //wifi,时间初始化

  //enableAll();
  spilcdFill(&lcd, 0, DRAW_TO_LCD); // erase display to black
  
} /* setup() */

void clearScr()
{
  spilcdFill(&lcd, 0, DRAW_TO_LCD); // erase display to black
}

void printBoth(const char* str)
{
  Serial.print(str);
  spilcdWriteString(&lcd,0,50,(char *)str,0x7e0,0,FONT_8x8, DRAW_TO_LCD);
}

void resetDisps()
{
  digitalWrite(TFT_RST,HIGH);
  digitalWrite(TFT_RST,LOW);
  delay(100);
  digitalWrite(TFT_RST,HIGH);
  
}



//#define SHOWJPG(img,x,y) showjpg((uint8_t *) img,sizeof(img),x,y)

void showjpg(uint8_t * pjpg_img,int size,int x=0,int y=0)
{
  long lTime;
  char szTemp[64];

  // Open a large JPEG image stored in FLASH memory (included as thumb_test.h)
  // This image is 12 megapixels, but has a 320x240 embedded thumbnail in it
  if (jpeg.openFLASH((uint8_t *)pjpg_img, size, drawMCUs))
  {
    //Serial.println("Successfully opened JPEG image");
    //Serial.printf("Image size: %d x %d, orientation: %d, bpp: %d\n", jpeg.getWidth(),
    //  jpeg.getHeight(), jpeg.getOrientation(), jpeg.getBpp());

    // if (jpeg.hasThumb())
    //    Serial.printf("Thumbnail present: %d x %d\n", jpeg.getThumbWidth(), jpeg.getThumbHeight());
    jpeg.setPixelType(RGB565_BIG_ENDIAN); // The SPI LCD wants the 16-bit pixels in big-endian order
    lTime = micros();
    // Draw the thumbnail image in the middle of the display (upper left corner = 120,100) at 1/4 scale
    //if (jpeg.decode(x,y,JPEG_AUTO_ROTATE))
    if (jpeg.decode(x,y,0))
    {
       lTime = micros() - lTime;
       sprintf(szTemp, "Successfully decoded image in %d us", (int)lTime);
    //   //Serial.println(szTemp);
    //   //spilcdWriteString(&lcd, 0, 200, szTemp, 0xffe0, 0, FONT_8x8, DRAW_TO_LCD);
    }
    jpeg.close();
  }
  else
    Serial.println("open jpg failed.");
}

void showimg(int i)
{
  showjpg((uint8_t*) pics[i].img,pics[i].size);
}

int ic=0;
int firstShow=0;

void loop() {
  ic=(ic+1)%3000;
  clearScr();
  Serial.println("pic numbers:");
  Serial.println(PicNum);
  showimg(ic%(PicNum));
  delay(3000);
}




